﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DCT.Data;
using System.Data;
using DCT.Entities;

public partial class Admin_Hinh_Anh_QLHinhAnh : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //BindCatalog.BindBoSuuTap(cboBoSuuTap);
            Pager1.CurrentIndex = 1;
            Bind_Data(Pager1.CurrentIndex);
        }
    }

    protected void Bind_Data(int PageNo)
    {
        int TotalRows = 0;
        string WhereClause = "1=1";
        //if (cboBoSuuTap.SelectedValue != "0")
        //    WhereClause += " AND IdBoSuuTap=" + cboBoSuuTap.SelectedValue;
        if (Common.StandardString(txtTen.Text).Length > 0)
            WhereClause += " AND DienGiai LIKE '%" + Common.StandardString(txtTen.Text) + "%'";
        TList<HinhAnh> TList_Catalog = DataRepository.HinhAnhProvider.GetPaged(WhereClause, "NgayCapNhat DESC", PageNo - 1, Pager1.PageSize, out TotalRows);
        dlHinhAnh.DataSource = TList_Catalog;
        dlHinhAnh.DataBind();

        Pager1.ItemCount = TotalRows;
        if (TotalRows <= Pager1.PageSize)
            Pager1.Visible = false;
        else
            Pager1.Visible = true;
    }

    public string GetHinhAnh(object IdBoSuuTap, object HinhAnh)
    {
        return Common.sPathUpBoSuuTap + "/" + IdBoSuuTap + "/Thumb/" + HinhAnh;
    }

    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        try
        {
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            Pager1.CurrentIndex = currnetPageIndex;
            Bind_Data(currnetPageIndex);
        }
        catch (Exception ex)
        {
            Common.WriteLog("QLHinhAnh.aspx.cs", "Pager1_Command()", ex.ToString());
        }
    }

    protected void cmdTimKiem_Click(object sender, ImageClickEventArgs e)
    {
        Pager1.CurrentIndex = 1;
        Bind_Data(Pager1.CurrentIndex);
    }

    protected void cmdTimLai_Click(object sender, ImageClickEventArgs e)
    {
        //cboBoSuuTap.SelectedValue = "0";
        txtTen.Text = "";
        Pager1.CurrentIndex = 1;
        Bind_Data(Pager1.CurrentIndex);
    }

    protected void dlHinhAnh_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.Equals("Delete"))
        {
            int IdHinhAnh = Convert.ToInt32(e.CommandArgument.ToString());
            HinhAnh entHinhAnh = DataRepository.HinhAnhProvider.GetById(IdHinhAnh);
            if (entHinhAnh != null)
            {
                string sFilePath = Server.MapPath(Common.sPathUpBoSuuTap);
                string sThumbPath = sFilePath + "\\" + entHinhAnh.Id.ToString() + "\\Thumb\\" + entHinhAnh.DienGiai;
                string sFullPath = sFilePath + "\\" + entHinhAnh.Id.ToString() + "\\Full\\" + entHinhAnh.DienGiai;
                if (System.IO.File.Exists(sThumbPath))
                {
                    System.IO.File.Delete(sThumbPath);
                }
                if (System.IO.File.Exists(sFullPath))
                {
                    System.IO.File.Delete(sFullPath);
                }
                DataRepository.HinhAnhProvider.Delete(IdHinhAnh);
                Bind_Data(Pager1.CurrentIndex);
            }
        }
    }
}
